<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>django.utils.module_loading &mdash; Django 1.7.8.dev20150401230226 documentation</title>
    
    <link rel="stylesheet" href="../../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../../',
        VERSION:     '1.7.8.dev20150401230226',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../../_static/doctools.js"></script>
    <link rel="top" title="Django 1.7.8.dev20150401230226 documentation" href="../../../index.html" />
    <link rel="up" title="django" href="../../django.html" />



 
<script type="text/javascript" src="../../../templatebuiltins.js"></script>
<script type="text/javascript">
(function($) {
    if (!django_template_builtins) {
       // templatebuiltins.js missing, do nothing.
       return;
    }
    $(document).ready(function() {
        // Hyperlink Django template tags and filters
        var base = "../../../ref/templates/builtins.html";
        if (base == "#") {
            // Special case for builtins.html itself
            base = "";
        }
        // Tags are keywords, class '.k'
        $("div.highlight\\-html\\+django span.k").each(function(i, elem) {
             var tagname = $(elem).text();
             if ($.inArray(tagname, django_template_builtins.ttags) != -1) {
                 var fragment = tagname.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + tagname + "</a>");
             }
        });
        // Filters are functions, class '.nf'
        $("div.highlight\\-html\\+django span.nf").each(function(i, elem) {
             var filtername = $(elem).text();
             if ($.inArray(filtername, django_template_builtins.tfilters) != -1) {
                 var fragment = filtername.replace(/_/, '-');
                 $(elem).html("<a href='" + base + "#" + fragment + "'>" + filtername + "</a>");
             }
        });
    });
})(jQuery);
</script>


  </head>
  <body>

    <div class="document">
  <div id="custom-doc" class="yui-t6">
    <div id="hd">
      <h1><a href="../../../index.html">Django 1.7.8.dev20150401230226 documentation</a></h1>
      <div id="global-nav">
        <a title="Home page" href="../../../index.html">Home</a>  |
        <a title="Table of contents" href="../../../contents.html">Table of contents</a>  |
        <a title="Global index" href="../../../genindex.html">Index</a>  |
        <a title="Module index" href="../../../py-modindex.html">Modules</a>
      </div>
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>

    <div id="bd">
      <div id="yui-main">
        <div class="yui-b">
          <div class="yui-g" id="_modules-django-utils-module_loading">
            
  <h1>Source code for django.utils.module_loading</h1><div class="highlight"><pre>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="kn">import</span> <span class="n">absolute_import</span>  <span class="c"># Avoid importing `importlib` from this package.</span>

<span class="kn">import</span> <span class="nn">copy</span>
<span class="kn">import</span> <span class="nn">imp</span>
<span class="kn">from</span> <span class="nn">importlib</span> <span class="kn">import</span> <span class="n">import_module</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">warnings</span>

<span class="kn">from</span> <span class="nn">django.core.exceptions</span> <span class="kn">import</span> <span class="n">ImproperlyConfigured</span>
<span class="kn">from</span> <span class="nn">django.utils</span> <span class="kn">import</span> <span class="n">six</span>
<span class="kn">from</span> <span class="nn">django.utils.deprecation</span> <span class="kn">import</span> <span class="n">RemovedInDjango19Warning</span>


<div class="viewcode-block" id="import_string"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.module_loading.import_string">[docs]</a><span class="k">def</span> <span class="nf">import_string</span><span class="p">(</span><span class="n">dotted_path</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Import a dotted module path and return the attribute/class designated by the</span>
<span class="sd">    last name in the path. Raise ImportError if the import failed.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">module_path</span><span class="p">,</span> <span class="n">class_name</span> <span class="o">=</span> <span class="n">dotted_path</span><span class="o">.</span><span class="n">rsplit</span><span class="p">(</span><span class="s">&#39;.&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">ValueError</span><span class="p">:</span>
        <span class="n">msg</span> <span class="o">=</span> <span class="s">&quot;</span><span class="si">%s</span><span class="s"> doesn&#39;t look like a module path&quot;</span> <span class="o">%</span> <span class="n">dotted_path</span>
        <span class="n">six</span><span class="o">.</span><span class="n">reraise</span><span class="p">(</span><span class="ne">ImportError</span><span class="p">,</span> <span class="ne">ImportError</span><span class="p">(</span><span class="n">msg</span><span class="p">),</span> <span class="n">sys</span><span class="o">.</span><span class="n">exc_info</span><span class="p">()[</span><span class="mi">2</span><span class="p">])</span>

    <span class="n">module</span> <span class="o">=</span> <span class="n">import_module</span><span class="p">(</span><span class="n">module_path</span><span class="p">)</span>

    <span class="k">try</span><span class="p">:</span>
        <span class="k">return</span> <span class="nb">getattr</span><span class="p">(</span><span class="n">module</span><span class="p">,</span> <span class="n">class_name</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
        <span class="n">msg</span> <span class="o">=</span> <span class="s">&#39;Module &quot;</span><span class="si">%s</span><span class="s">&quot; does not define a &quot;</span><span class="si">%s</span><span class="s">&quot; attribute/class&#39;</span> <span class="o">%</span> <span class="p">(</span>
            <span class="n">dotted_path</span><span class="p">,</span> <span class="n">class_name</span><span class="p">)</span>
        <span class="n">six</span><span class="o">.</span><span class="n">reraise</span><span class="p">(</span><span class="ne">ImportError</span><span class="p">,</span> <span class="ne">ImportError</span><span class="p">(</span><span class="n">msg</span><span class="p">),</span> <span class="n">sys</span><span class="o">.</span><span class="n">exc_info</span><span class="p">()[</span><span class="mi">2</span><span class="p">])</span>

</div>
<div class="viewcode-block" id="import_by_path"><a class="viewcode-back" href="../../../ref/utils.html#django.utils.module_loading.import_by_path">[docs]</a><span class="k">def</span> <span class="nf">import_by_path</span><span class="p">(</span><span class="n">dotted_path</span><span class="p">,</span> <span class="n">error_prefix</span><span class="o">=</span><span class="s">&#39;&#39;</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Import a dotted module path and return the attribute/class designated by the</span>
<span class="sd">    last name in the path. Raise ImproperlyConfigured if something goes wrong.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="n">warnings</span><span class="o">.</span><span class="n">warn</span><span class="p">(</span>
        <span class="s">&#39;import_by_path() has been deprecated. Use import_string() instead.&#39;</span><span class="p">,</span>
        <span class="n">RemovedInDjango19Warning</span><span class="p">,</span> <span class="n">stacklevel</span><span class="o">=</span><span class="mi">2</span><span class="p">)</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">attr</span> <span class="o">=</span> <span class="n">import_string</span><span class="p">(</span><span class="n">dotted_path</span><span class="p">)</span>
    <span class="k">except</span> <span class="ne">ImportError</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
        <span class="n">msg</span> <span class="o">=</span> <span class="s">&#39;</span><span class="si">%s</span><span class="s">Error importing module </span><span class="si">%s</span><span class="s">: &quot;</span><span class="si">%s</span><span class="s">&quot;&#39;</span> <span class="o">%</span> <span class="p">(</span>
            <span class="n">error_prefix</span><span class="p">,</span> <span class="n">dotted_path</span><span class="p">,</span> <span class="n">e</span><span class="p">)</span>
        <span class="n">six</span><span class="o">.</span><span class="n">reraise</span><span class="p">(</span><span class="n">ImproperlyConfigured</span><span class="p">,</span> <span class="n">ImproperlyConfigured</span><span class="p">(</span><span class="n">msg</span><span class="p">),</span>
                    <span class="n">sys</span><span class="o">.</span><span class="n">exc_info</span><span class="p">()[</span><span class="mi">2</span><span class="p">])</span>
    <span class="k">return</span> <span class="n">attr</span>

</div>
<span class="k">def</span> <span class="nf">autodiscover_modules</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">    Auto-discover INSTALLED_APPS modules and fail silently when</span>
<span class="sd">    not present. This forces an import on them to register any admin bits they</span>
<span class="sd">    may want.</span>

<span class="sd">    You may provide a register_to keyword parameter as a way to access a</span>
<span class="sd">    registry. This register_to object must have a _registry instance variable</span>
<span class="sd">    to access it.</span>
<span class="sd">    &quot;&quot;&quot;</span>
    <span class="kn">from</span> <span class="nn">django.apps</span> <span class="kn">import</span> <span class="n">apps</span>

    <span class="n">register_to</span> <span class="o">=</span> <span class="n">kwargs</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="s">&#39;register_to&#39;</span><span class="p">)</span>
    <span class="k">for</span> <span class="n">app_config</span> <span class="ow">in</span> <span class="n">apps</span><span class="o">.</span><span class="n">get_app_configs</span><span class="p">():</span>
        <span class="c"># Attempt to import the app&#39;s module.</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">register_to</span><span class="p">:</span>
                <span class="n">before_import_registry</span> <span class="o">=</span> <span class="n">copy</span><span class="o">.</span><span class="n">copy</span><span class="p">(</span><span class="n">register_to</span><span class="o">.</span><span class="n">_registry</span><span class="p">)</span>

            <span class="k">for</span> <span class="n">module_to_search</span> <span class="ow">in</span> <span class="n">args</span><span class="p">:</span>
                <span class="n">import_module</span><span class="p">(</span><span class="s">&#39;</span><span class="si">%s</span><span class="s">.</span><span class="si">%s</span><span class="s">&#39;</span> <span class="o">%</span> <span class="p">(</span><span class="n">app_config</span><span class="o">.</span><span class="n">name</span><span class="p">,</span> <span class="n">module_to_search</span><span class="p">))</span>
        <span class="k">except</span><span class="p">:</span>
            <span class="c"># Reset the model registry to the state before the last import as</span>
            <span class="c"># this import will have to reoccur on the next request and this</span>
            <span class="c"># could raise NotRegistered and AlreadyRegistered exceptions</span>
            <span class="c"># (see #8245).</span>
            <span class="k">if</span> <span class="n">register_to</span><span class="p">:</span>
                <span class="n">register_to</span><span class="o">.</span><span class="n">_registry</span> <span class="o">=</span> <span class="n">before_import_registry</span>

            <span class="c"># Decide whether to bubble up this error. If the app just</span>
            <span class="c"># doesn&#39;t have an admin module, we can ignore the error</span>
            <span class="c"># attempting to import it, otherwise we want it to bubble up.</span>
            <span class="k">if</span> <span class="n">module_has_submodule</span><span class="p">(</span><span class="n">app_config</span><span class="o">.</span><span class="n">module</span><span class="p">,</span> <span class="n">module_to_search</span><span class="p">):</span>
                <span class="k">raise</span>


<span class="k">def</span> <span class="nf">module_has_submodule</span><span class="p">(</span><span class="n">package</span><span class="p">,</span> <span class="n">module_name</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;See if &#39;module&#39; is in &#39;package&#39;.&quot;&quot;&quot;</span>
    <span class="n">name</span> <span class="o">=</span> <span class="s">&quot;.&quot;</span><span class="o">.</span><span class="n">join</span><span class="p">([</span><span class="n">package</span><span class="o">.</span><span class="n">__name__</span><span class="p">,</span> <span class="n">module_name</span><span class="p">])</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="c"># None indicates a cached miss; see mark_miss() in Python/import.c.</span>
        <span class="k">return</span> <span class="n">sys</span><span class="o">.</span><span class="n">modules</span><span class="p">[</span><span class="n">name</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span>
    <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
        <span class="k">pass</span>
    <span class="k">try</span><span class="p">:</span>
        <span class="n">package_path</span> <span class="o">=</span> <span class="n">package</span><span class="o">.</span><span class="n">__path__</span>   <span class="c"># No __path__, then not a package.</span>
    <span class="k">except</span> <span class="ne">AttributeError</span><span class="p">:</span>
        <span class="c"># Since the remainder of this function assumes that we&#39;re dealing with</span>
        <span class="c"># a package (module with a __path__), so if it&#39;s not, then bail here.</span>
        <span class="k">return</span> <span class="bp">False</span>
    <span class="k">for</span> <span class="n">finder</span> <span class="ow">in</span> <span class="n">sys</span><span class="o">.</span><span class="n">meta_path</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">finder</span><span class="o">.</span><span class="n">find_module</span><span class="p">(</span><span class="n">name</span><span class="p">,</span> <span class="n">package_path</span><span class="p">):</span>
            <span class="k">return</span> <span class="bp">True</span>
    <span class="k">for</span> <span class="n">entry</span> <span class="ow">in</span> <span class="n">package_path</span><span class="p">:</span>
        <span class="k">try</span><span class="p">:</span>
            <span class="c"># Try the cached finder.</span>
            <span class="n">finder</span> <span class="o">=</span> <span class="n">sys</span><span class="o">.</span><span class="n">path_importer_cache</span><span class="p">[</span><span class="n">entry</span><span class="p">]</span>
            <span class="k">if</span> <span class="n">finder</span> <span class="ow">is</span> <span class="bp">None</span><span class="p">:</span>
                <span class="c"># Implicit import machinery should be used.</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">file_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">imp</span><span class="o">.</span><span class="n">find_module</span><span class="p">(</span><span class="n">module_name</span><span class="p">,</span> <span class="p">[</span><span class="n">entry</span><span class="p">])</span>
                    <span class="k">if</span> <span class="n">file_</span><span class="p">:</span>
                        <span class="n">file_</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
                    <span class="k">return</span> <span class="bp">True</span>
                <span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
                    <span class="k">continue</span>
            <span class="c"># Else see if the finder knows of a loader.</span>
            <span class="k">elif</span> <span class="n">finder</span><span class="o">.</span><span class="n">find_module</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
                <span class="k">return</span> <span class="bp">True</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="k">continue</span>
        <span class="k">except</span> <span class="ne">KeyError</span><span class="p">:</span>
            <span class="c"># No cached finder, so try and make one.</span>
            <span class="k">for</span> <span class="n">hook</span> <span class="ow">in</span> <span class="n">sys</span><span class="o">.</span><span class="n">path_hooks</span><span class="p">:</span>
                <span class="k">try</span><span class="p">:</span>
                    <span class="n">finder</span> <span class="o">=</span> <span class="n">hook</span><span class="p">(</span><span class="n">entry</span><span class="p">)</span>
                    <span class="c"># XXX Could cache in sys.path_importer_cache</span>
                    <span class="k">if</span> <span class="n">finder</span><span class="o">.</span><span class="n">find_module</span><span class="p">(</span><span class="n">name</span><span class="p">):</span>
                        <span class="k">return</span> <span class="bp">True</span>
                    <span class="k">else</span><span class="p">:</span>
                        <span class="c"># Once a finder is found, stop the search.</span>
                        <span class="k">break</span>
                <span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
                    <span class="c"># Continue the search for a finder.</span>
                    <span class="k">continue</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c"># No finder found.</span>
                <span class="c"># Try the implicit import machinery if searching a directory.</span>
                <span class="k">if</span> <span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">isdir</span><span class="p">(</span><span class="n">entry</span><span class="p">):</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="n">file_</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">_</span> <span class="o">=</span> <span class="n">imp</span><span class="o">.</span><span class="n">find_module</span><span class="p">(</span><span class="n">module_name</span><span class="p">,</span> <span class="p">[</span><span class="n">entry</span><span class="p">])</span>
                        <span class="k">if</span> <span class="n">file_</span><span class="p">:</span>
                            <span class="n">file_</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
                        <span class="k">return</span> <span class="bp">True</span>
                    <span class="k">except</span> <span class="ne">ImportError</span><span class="p">:</span>
                        <span class="k">pass</span>
                <span class="c"># XXX Could insert None or NullImporter</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="c"># Exhausted the search, so the module cannot be found.</span>
        <span class="k">return</span> <span class="bp">False</span>
</pre></div>

          </div>
        </div>
      </div>
      
        
          <div class="yui-b" id="sidebar">
            
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3>Browse</h3>
  <ul>
    
    
  </ul>
  <h3>You are here:</h3>
  <ul>
      <li>
        <a href="../../../index.html">Django 1.7.8.dev20150401230226 documentation</a>
        
          <ul><li><a href="../../index.html">Module code</a>
        
          <ul><li><a href="../../django.html">django</a>
        
        <ul><li>django.utils.module_loading</li></ul>
        </li></ul></li></ul>
      </li>
  </ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
              <h3>Last update:</h3>
              <p class="topless">Apr 02, 2015</p>
          </div>
        
      
    </div>

    <div id="ft">
      <div class="nav">
    <a href="../../index.html" title="Module code" accesskey="U">up</a></div>
    </div>
  </div>

      <div class="clearer"></div>
    </div>
  </body>
</html>